package com.tth.learn.addproperties.java;

/**
 * 创建两个线程。一个遍历100以内的偶数，一个遍历100以内的奇数
 *
 * @Author tth
 * @Date 2022/10/28 14:39
 */
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        MyThread1 thread1 = new MyThread1();
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();

        Thread.currentThread().setName("主线程");
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + "_" + Thread.currentThread().getPriority() + ":" + i);
            }

            if (i == 21) {
//                thread1.join();
            }
        }
    }

}

class MyThread1 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {

            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + "_" + Thread.currentThread().getPriority() + ":" + i);
            }

            if (i % 20 == 0) {
                yield();
            }

        }
    }
}

class MyThread2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}